## Process this file with autoconf to produce a configure script.
##

AC_PREREQ([2.69])
AC_INIT([rtems-c-src],[_RTEMS_VERSION],[https://devel.rtems.org/newticket])
AC_CONFIG_SRCDIR([libchip])
RTEMS_TOP(../..)

RTEMS_ENABLE_CXX
RTEMS_ENABLE_NETWORKING

RTEMS_CANONICAL_TARGET_CPU

AM_INIT_AUTOMAKE([no-define subdir-objects no-exeext foreign 1.12.2])
AM_MAINTAINER_MODE

RTEMS_ENABLE_MULTILIB
RTEMS_ENABLE_MULTIPROCESSING
RTEMS_ENABLE_CXX
RTEMS_ENV_RTEMSBSP

RTEMS_CHECK_CUSTOM_BSP(RTEMS_BSP)

RTEMS_CHECK_NETWORKING(RTEMS_BSP)
RTEMS_CHECK_POSIX_API(RTEMS_BSP)

AC_MSG_NOTICE([setting up make/custom])

AS_MKDIR_P([make/custom])
AM_SET_DEPDIR
test -d ${DEPDIR} || mkdir ${DEPDIR}

rm -f ${DEPDIR}/cfg.P
cp ${srcdir}/make/custom/default.cfg.in make/custom/default.cfg
cat << EOF > ${DEPDIR}/cfg.P
# Do not edit - automatically generated by $0
make/custom/default.cfg: \$(srcdir)/make/custom/default.cfg.in
	@\$(MKDIR_P) make/custom
	cp \$(srcdir)/make/custom/default.cfg.in make/custom/default.cfg
EOF

# Collect and copy the BSP's make/custom/*.cfg files from the toplevel make
# directory into the build tree's <bsp>/make/custom subdirectories
CUSTOM_CFG_FILES=
f="${RTEMS_BSP}.cfg"
while test -n "$f"; do
  cfg_file=
  _RTEMS_CHECK_CUSTOM_BSP([$f],cfg_file)
  if test -n "${cfg_file}"; then
    cp ${cfg_file} make/custom/$f
cat << EOF >> ${DEPDIR}/cfg.P
make/custom/$f: ${cfg_file} make/custom/default.cfg
	cp ${cfg_file} make/custom/$f
EOF
    CUSTOM_CFG_FILES="$CUSTOM_CFG_FILES make/custom/$f"
    f=`grep "^include.*make/custom" ${cfg_file} \
    | sed \
      -e 's%^.*custom\/%%' \
      -e 's%default\.cfg%%'`;
  else break; fi;
done

AC_MSG_NOTICE([creating make/${RTEMS_BSP}.cache])
${MAKE-make} -f ${srcdir}/make/bsp.mak \
RTEMS_BSP=${RTEMS_BSP} \
RTEMS_ROOT=. make/${RTEMS_BSP}.cache

# Read back the cache
AC_CACHE_CHECK([for RTEMS_CPU_MODEL], [ac_cv_env_RTEMS_CPU_MODEL],
[. ./make/${RTEMS_BSP}.cache])
RTEMS_CPU_MODEL=$ac_cv_env_RTEMS_CPU_MODEL
AC_SUBST(RTEMS_CPU_MODEL)
test -n "${RTEMS_CPU_MODEL}" && \
  ac_configure_args="$ac_configure_args 'RTEMS_CPU_MODEL=${RTEMS_CPU_MODEL}'"

AC_CACHE_CHECK([for RTEMS_BSP_FAMILY],[ac_cv_env_RTEMS_BSP_FAMILY],
[RTEMS_BSP_ALIAS([${RTEMS_BSP}],[ac_cv_env_RTEMS_BSP_FAMILY])])
RTEMS_BSP_FAMILY=$ac_cv_env_RTEMS_BSP_FAMILY
AC_SUBST(RTEMS_BSP_FAMILY)
test -n "${RTEMS_BSP_FAMILY}" && \
  ac_configure_args="$ac_configure_args 'RTEMS_BSP_FAMILY=${RTEMS_BSP_FAMILY}'"

AC_CACHE_CHECK([for CPU_CFLAGS],[ac_cv_env_CPU_CFLAGS],
[. ./make/${RTEMS_BSP}.cache])
CPU_CFLAGS=$ac_cv_env_CPU_CFLAGS

AC_CACHE_CHECK([for CFLAGS_OPTIMIZE_V],[ac_cv_env_CFLAGS_OPTIMIZE_V],
[. ./make/${RTEMS_BSP}.cache])
CFLAGS_OPTIMIZE_V=$ac_cv_env_CFLAGS_OPTIMIZE_V

# Was CFLAGS set?
rtems_cv_CFLAGS_set="${CFLAGS+set}"

CFLAGS="${CFLAGS-${CPU_CFLAGS} ${CFLAGS_OPTIMIZE_V}}"

RTEMS_PROG_CC_FOR_TARGET
RTEMS_PROG_CXX_FOR_TARGET
AM_PROG_CC_C_O
RTEMS_PROG_CCAS
RTEMS_CANONICALIZE_TOOLS

# Append warning flags if CFLAGS wasn't set.
AS_IF([test "$GCC" = yes && test "$rtems_cv_CFLAGS_set" != set],
[CFLAGS="$CFLAGS -Wall -Wmissing-prototypes -Wimplicit-function-declaration -Wstrict-prototypes -Wnested-externs"])

AS_IF([test -n "${CFLAGS}"],[
ac_configure_args="$ac_configure_args 'CFLAGS=${CFLAGS}'"])

RTEMS_CHECK_GCC_WEAK

AC_SUBST(CUSTOM_CFG_FILES)

RTEMS_CHECK_MULTIPROCESSING

BSP_SUBDIRS=
## Configure ${srcdir}/../../cpukit as cpukit/ if multilibs are disabled
RTEMS_BSP_CONFIG_SUBDIR(
  [cpukit],[../../cpukit],
  ['--with-project-root=${with_project_root}../$RTEMS_BSP/' \
   '--includedir=${exec_prefix}/${RTEMS_BSP}/lib/include' \
   '--libdir=${exec_prefix}/${RTEMS_BSP}/lib' \
  ],
  [test x"$multilib" = xno])

## Note: the order of the directories below is essential
AC_CONFIG_SUBDIRS([make])
BSP_SUBDIRS="$BSP_SUBDIRS make"

RTEMS_CPU_SUBDIRS(lib/libcpu)
if test -d "${srcdir}/lib/libcpu/${RTEMS_CPU}"; then
  libcpu_cpu_subdir="${RTEMS_CPU}"
else
  libcpu_cpu_subdir=
fi
AC_SUBST(libcpu_cpu_subdir)

# Is there code where there should be for this BSP?
RTEMS_CPU_SUBDIRS([lib/libbsp])
AC_SUBST(libbsp_cpu_subdir,$RTEMS_CPU)

BSP_SUBDIRS="$BSP_SUBDIRS lib"
BSP_SUBDIRS="$BSP_SUBDIRS libchip"
BSP_SUBDIRS="$BSP_SUBDIRS support"
BSP_SUBDIRS="$BSP_SUBDIRS ada"
BSP_SUBDIRS="$BSP_SUBDIRS wrapup"

AC_ARG_ENABLE([ada],
[AS_HELP_STRING(--enable-ada,enable ada support)],
[case "${enable_ada}" in
  yes) ;;
  no) ;;
  *)  AC_MSG_ERROR(bad value ${enable_ada} for --enable-ada) ;;
esac],[enable_ada=no])

RTEMS_BSP_CONFIG_SUBDIR(
  [ada-tests],[ada-tests],
  ['--enable-rtems-root=../' \
   '--enable-project-root=${with_project_root}../$RTEMS_BSP' \
   '--with-project-top=${with_project_top}../' \
   '--enable-rtemsbsp=$RTEMS_BSP' \
  ],
  [test x"$enable_tests" = xyes && test x"$enable_ada" = xyes])

# HACK ALERT!

# FIXME: Assume multilib implies in-source-tree multilibs
# Build testsuites multilibbed
RTEMS_BSP_CONFIG_SUBDIR(
  [testsuites],[../../testsuites],
  ['--enable-rtems-root=../' \
   '--enable-project-root=${with_project_root}../$RTEMS_BSP' \
   '--with-project-top=${with_project_top}../' \
   '--enable-rtemsbsp=$RTEMS_BSP' \
   '--includedir=${exec_prefix}/${RTEMS_BSP}/lib/include' \
   '--libdir=${exec_prefix}/${RTEMS_BSP}/lib' \
  ],
  [test x"$enable_tests" != x"no" && test x"$multilib" = xno])

# Build testsuites non-multilibbed
RTEMS_BSP_CONFIG_SUBDIR(
  [testsuites],[../../testsuites],
  ['--enable-rtems-root=../' \
   '--enable-cpukit-root=${with_project_root}..' \
   '--enable-project-root=${with_project_root}../$RTEMS_BSP' \
   '--with-project-top=${with_project_top}../' \
   '--enable-rtemsbsp=$RTEMS_BSP' \
   '--includedir=${exec_prefix}/${RTEMS_BSP}/lib/include' \
   '--libdir=${exec_prefix}/${RTEMS_BSP}/lib' \
  ],
  [test x"$enable_tests" != x"no" && test x"$multilib" = xyes])

AC_SUBST([BSP_SUBDIRS],[$BSP_SUBDIRS])

AS_IF([test "$host_cpu" = sparc],[
  AC_DEFINE_UNQUOTED([CPU_U32_FIX],[1],
    [whether to enable ipalignment work-around])
])

AM_CONDITIONAL([HAS_NETWORKING],[test "$HAS_NETWORKING" = "yes"])

AM_CONDITIONAL(RTEMS_ADA,[test x"$enable_ada" = x"yes" \
  && test x"$rtems_cv_HAS_POSIX_API" = x"yes"])

RTEMS_PROJECT_ROOT

# Explicitly list all Makefiles here
AC_CONFIG_FILES([Makefile],
[${MAKE} make/${RTEMS_BSP}.cache],
[RTEMS_BSP=${RTEMS_BSP}
 MAKE=${MAKE}])

AC_CONFIG_FILES([
support/Makefile
libchip/Makefile
lib/Makefile
lib/libcpu/Makefile
lib/libbsp/Makefile
ada/Makefile
wrapup/Makefile
])

AC_OUTPUT
